package com.ecommerce.source;
import com.ecommerce.model.SearchWordData;import org.apache.flink.streaming.api.functions.source.RichSourceFunction;
import java.util.Random;
public class SearchWordDataSource extends RichSourceFunction<SearchWordData> {
    private volatile boolean running = true;
    private final Random random = new Random();
    private final String[] categories = {"女装", "男装", "美妆", "数码", "家居", "食品", "母婴", "运动"};
    private final String[] wordTypes = {"搜索词", "趋势词", "核心词", "修饰词"};
    private final String[] keywords = {"连衣裙", "手机", "面膜", "沙发", "奶粉", "跑步鞋", "口红", "笔记本电脑"};
    
    @Override
    public void run(SourceContext<SearchWordData> ctx) throws Exception {
        while (running) {
            for (int i = 0; i < 50; i++) {
                SearchWordData word = new SearchWordData();
                word.setKeyword(keywords[random.nextInt(keywords.length)] + "_" + random.nextInt(100));
                word.setWordType(wordTypes[random.nextInt(wordTypes.length)]);
                word.setSearchCount(random.nextInt(100000));
                word.setGrowthRate(random.nextDouble() * 3 - 1); // -1 到 2
                word.setSupplyDemandRatio(random.nextDouble() * 5);
                word.setCategory(categories[random.nextInt(categories.length)]);
                word.setTimestamp(System.currentTimeMillis());
                
                ctx.collect(word);
            }
            Thread.sleep(2000); // 每2秒生成50条数据
        }
    }
    
    @Override
    public void cancel() {
        running = false;
    }}